/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.demo.guava;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.primitives.Ints;
import org.junit.Test;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
 * @author vacoor
 */
public class Demo {
    @Test
    public void testCharMatcher() {
        CharMatcher matcher = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'));
        boolean m = matcher.matches('M');
        assertEquals(m, true);

        assertEquals(false, CharMatcher.DIGIT.matches('a'));
        assertEquals("中文abc", CharMatcher.JAVA_LETTER.retainFrom("中文a1b2c3"));
        assertEquals("abc", CharMatcher.DIGIT.removeFrom("a1b2c3"));
    }

    @Test
    public void testJoiner() {
        Integer[] a = {1, 2, null, 123};
        String join = Joiner.on(',').skipNulls().join(a);
        assertEquals("1,2,123", join);
        //Ints.join()
    }

    @Test
    public void testSplitter() {
        Iterable<String> split = Splitter.on(",").omitEmptyStrings().trimResults().split("1, 2, 3, , 5");
        for (String s : split) {
            System.out.println(s);
        }
    }

    @Test
    public void testSets() {
        Set<Integer> set1 = Sets.newHashSet(1, 2, 3);
        Set<Integer> set2 = Sets.newHashSet(1, 3);

        Sets.SetView<Integer> set = Sets.union(set1, set2);
        assertArrayEquals(new Object[]{1, 2, 3}, set.toArray());

        // Sets.difference() 差集
        //Sets.intersection() 交集
    }

    @Test
    public void test() {
        // 不可变
        ImmutableMap<Integer, String> of = ImmutableMap.of(1, "age", 2, "name");
    }

    @Test
    public void testCollection2() {
        List<Integer> list = Lists.newArrayList(1, 2, 3);
        int[] ints = Ints.toArray(list);
    }

    // ------ map

    @Test
    public void testMultiMap() {
        Multimap<Integer, String> multimap = HashMultimap.create();
        multimap.put(1, "name");
        multimap.put(2, "age");
        multimap.put(1, "lastname");

        Collection<String> strings = multimap.get(1);
        assertEquals(strings.size(), 2);

        // 删除其中一个
        multimap.remove(1, "name");
        strings = multimap.get(1);
        assertEquals(strings.size(), 1);

        // multimap.asMap();
    }

    // 双向map
    @Test
    public void testBiMap() {
        BiMap<Integer, String> biMap = HashBiMap.create();
        BiMap<String, Integer> inverse = biMap.inverse();

        biMap.put(1, "value");
        assertEquals((Object) inverse.get("value"), 1);

        // inverse 中设置的 bimap中也能取得
        inverse.put("name", 2);
        assertEquals(biMap.get(2), "name");
    }

    @Test
    public void testTable() {
        Table<Integer, String, String> table = HashBasedTable.create();
        table.put(1, "Name", "Zhangsan");
        table.put(2, "Age", "11");

        assertEquals("11", table.get(2, "Age"));
    }

}
